package com.dgut.forum.services.impl;

import com.dgut.forum.common.AppResult;
import com.dgut.forum.common.ResultCode;
import com.dgut.forum.dao.BoardMapper;
import com.dgut.forum.exception.ApplicationException;
import com.dgut.forum.model.Board;
import com.dgut.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<0){
            log.warn("参数校验失败,要求展示版块数小于0");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards=boardMapper.selectByNum(num);

        return boards;
    }
    /*
     * 新增文章时在文章新增之后修改版块的文章数量
     *
     * */
    @Override
    public void addOneToBoardArticleCount(Long id) {
        if(id==null||id<0){
            log.warn("新增版块文章数量失败,id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ADD_COUNT));
        }
        Board board=boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.info("新增板块文章数量时查不到版块信息,board id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int ret=boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(ret!=1){
            log.error("新增版块文章数量失败,board id="+id+" ,"+ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

        log.info("新增版块 board id="+id+"文章数量成功");
    }

    @Override
    public Board selectById(Long id) {
        if(id==null||id<0){
            log.warn("查询板块信息失败,id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board=boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void subOneToBoardArticleCount(Long id) {
        if(id==null||id<=0){
            log.warn("减少版块帖子数量时Id参数异常,Id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board=boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn("板块不存在,Id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard=new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        int row=boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            log.error("减少版块帖子数量失败,Id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info("减少版块帖子数量成功,版块Id="+id);
    }
}


